﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.IO;

public partial class S_InquiryPage : System.Web.UI.Page
{
    public static DataTable ta = new DataTable();//用于存储从数据库中取出来的表格，可以在多个函数中使用，所以使用static
    public static DataView vi = new DataView();  //用于数据绑定
    public static string accnum;           //登录账号
    public static string role;             //登录身份

    protected void Page_Load(object sender, EventArgs e)
    {
        btnFirst.Text = "最首页";    //分页处理
        btnPrev.Text = "前一页";
        btnNext.Text = "下一页";
        btnLast.Text = "最后页";

        if (!IsPostBack)
        {
            accnum = (string)Session["AccNum"];
            role = (string)Session["role"];
            ShowDataList();

            Execute("select courseid,COURSE_INFO.name as coursename,studentid,STU_INFO.name as studentname,mark,SEM_INFO.name as semester from SEM_INFO,CLASS_INFO,COURSE_INFO,STU_INFO where CLASS_INFO.courseid=COURSE_INFO.id and CLASS_INFO.studentid=STU_INFO.id and STU_INFO.id='"+accnum+"' and CLASS_INFO.semesterid=SEM_INFO.id and CLASS_INFO.state='1';");
            //执行查询语句，默认状态下加载页面时显示浏览选课信息，此处还没有根据登录教师id确定响应选课信息，而是选定整个COURSE_INFO表，需做进一步处理

        }
    }

    private void ShowStats()  //用于分页处理
    {
        lblCurrentIndex.Text = "第 " + (dg1.CurrentPageIndex + 1).ToString() + " 页";
        lblPageCount.Text = "总共 " + dg1.PageCount.ToString() + " 页";
    }

    public void PagerButtonClick(object sender, EventArgs e) //用于分页处理
    {
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            case "next":
                if (dg1.CurrentPageIndex < (dg1.PageCount - 1))
                {
                    dg1.CurrentPageIndex += 1;
                }
                break;
            case "prev":
                if (dg1.CurrentPageIndex > 0)
                {
                    dg1.CurrentPageIndex -= 1;
                }
                break;
            case "last":
                dg1.CurrentPageIndex = (dg1.PageCount - 1);
                break;
            default:
                dg1.CurrentPageIndex = System.Convert.ToInt32(arg);
                break;
        }
        vi = new DataView(ta);

        dg1.DataSource = vi;
        dg1.DataBind();
        ShowStats();
    }

    public void MyDataGrid_Page(Object source, DataGridPageChangedEventArgs e)  //用于分页处理
    {
        int startIndex;
        startIndex = dg1.CurrentPageIndex * dg1.PageSize;
        dg1.CurrentPageIndex = e.NewPageIndex;
        //BindGrid();
        vi = new DataView(ta);

        dg1.DataSource = vi;
        dg1.DataBind();
        ShowStats();

    }

    protected void LinkButton2_Click(object sender, EventArgs e)//浏览个人基本信息
    {

        ShowDataList();
        dg1.CurrentPageIndex = 0;

        Execute("SELECT * FROM STU_INFO where id='"+accnum+"'");//查询语句未对登录学生账号进行处理，需进一步处理


    }
    protected void LinkButton1_Click(object sender, EventArgs e)//浏览选课信息
    {

        ShowDataList();
        dg1.CurrentPageIndex = 0;

        Execute("select courseid,COURSE_INFO.name as coursename,studentid,STU_INFO.name as studentname,mark,SEM_INFO.name as semester from SEM_INFO,CLASS_INFO,COURSE_INFO,STU_INFO where CLASS_INFO.courseid=COURSE_INFO.id and CLASS_INFO.studentid=STU_INFO.id and STU_INFO.id='"+accnum+"' and CLASS_INFO.semesterid=SEM_INFO.id and CLASS_INFO.state='1';");
        //尚未针对登录学生账号进行处理
    }

    protected void LinkButton3_Click(object sender, EventArgs e)//课程查询
    {
        HideDataList();
        dg1.CurrentPageIndex = 0;

        DL1.Text = "请选择查询方式";
        DL2.Text = "请选择学期";
        TextBox1.Text = "";
    }
    
    public void ShowDataList()
    {
        dg1.Visible = true;
        btnFirst.Visible = true;
        btnPrev.Visible = true;
        btnNext.Visible = true;
        btnLast.Visible = true;
        lblPageCount.Visible = true;
        lblCurrentIndex.Visible = true;
        Panel1.Visible = false;
        
    }
    public void HideDataList()
    {
        dg1.Visible = false;
        btnFirst.Visible = false;
        btnPrev.Visible = false;
        btnNext.Visible = false;
        btnLast.Visible = false;
        lblPageCount.Visible = false;
        lblCurrentIndex.Visible = false;
        Panel1.Visible = true;
        
    }

    protected void Button1_Click(object sender, EventArgs e)//缺少登录学号的确定，缺少数据不存在，返回空表的提示
    {
        string sql = "";

        if (DL1.Text == "请选择查询方式")
        {
            Alert("请选择查询方式");
        }
        else if (DL1.Text == "基本信息查询")
        {
            if (DL2.Text == "请选择学期")
            {
                Alert("请选择学期");
            }
            else
            {
                if (TextBox1.Text == "")
                {
                    Alert("请输入课程名");
                }
                else
                {
                    ShowDataList();
                    sql = "SELECT DISTINCT COURSE_INFO.id as courseid,COURSE_INFO.name as coursename,COL_INFO.name as collegename,TEA_INFO.name as teachername,time,room,credit,SEM_INFO.name as semester FROM COURSE_INFO,CLASS_INFO,SEM_INFO,COL_INFO,TEA_INFO WHERE CLASS_INFO.courseid=COURSE_INFO.id AND COURSE_INFO.name='" + TextBox1.Text + "' and SEM_INFO.id=CLASS_INFO.semesterid and SEM_INFO.name='" + DL2.Text + "' and COURSE_INFO.collegeid=COL_INFO.id and COURSE_INFO.teacherid=TEA_INFO.id and COURSE_INFO.state='1';";
                    Execute(sql);
                }
            }
        }
        else if (DL1.Text == "选课查询")
        {
            if (DL2.Text == "请选择学期")
            {
                Alert("请选择学期");
            }
            else
            {
                if (TextBox1.Text == "")
                {
                    Alert("请输入课程名");
                }
                else
                {
                    ShowDataList();
                    sql = "select courseid,COURSE_INFO.name as coursename,studentid,STU_INFO.name as studentname,mark,SEM_INFO.name as semester from SEM_INFO,CLASS_INFO,COURSE_INFO,STU_INFO where CLASS_INFO.courseid=COURSE_INFO.id and COURSE_INFO.name='"+TextBox1.Text+"' and CLASS_INFO.studentid=STU_INFO.id and STU_INFO.id='"+accnum+"' and CLASS_INFO.semesterid=SEM_INFO.id and SEM_INFO.name='"+DL2.Text+"' and CLASS_INFO.state='1';";
                    Execute(sql);
                }

            }
        }



    }

    void Execute(string sql)//缺少查询信息不存在的出错处理，即返回空表，（上面的执行语句都可用此函数替代）
    {
        SqlConnection myConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=CSDB");
        SqlCommand cmd = new SqlCommand(sql, myConn);
        myConn.Open();

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        ta.Columns.Clear();
        ta.Rows.Clear();
        da.Fill(ta);
        vi = new DataView(ta);
        dg1.DataSource = vi;
        dg1.DataBind();
        myConn.Close();
    }

    void Alert(string message)//用于测试，无实际意义
    {
        Response.Write("<script>alert('" + message + "')</script>");
    }

}
